package cn.xuqiudong.blog.module.blog.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.blog.model.BlogArticle;
import cn.xuqiudong.common.blog.search.helper.SearchBlogHelper;
import cn.xuqiudong.common.blog.search.model.SearchBlogArticleModel;
import cn.xuqiudong.common.blog.search.service.SearchBlogService;
import cn.xuqiudong.common.blog.service.BlogArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2024-06-17 17:21
 */
@RestController
@RequestMapping("/test")
public class BlogTestController {
    private SearchBlogHelper searchBlogHelper;

    @Resource
    private BlogArticleService blogArticleService;

    /**
     * 原来的是EsBlogArticleService
     */
    @Autowired
    private SearchBlogService searchBlogService;

    /**
     * 测试初始化全部索引
     */
    @GetMapping("/search/init")
    public BaseResponse<?> initSearch() {
        int num = blogArticleService.initSearchDocument();
        return BaseResponse.success(num);
    }
    /**
     * 测试初始化某个文章的索引到meilisearch
     */
    @GetMapping("/search/save/{id}")
    public BaseResponse<?> saveSearch(@PathVariable int id) {
        BlogArticle article = blogArticleService.findById(id);
        if (article == null) {
            return BaseResponse.error("文章不存在");
        }
        searchBlogService.save(article);
        return BaseResponse.success();
    }

}
